package com.bigegg.employee.service.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.bigegg.employee.domain.*;
import com.bigegg.employee.mapper.ClassesMapper;
import com.bigegg.employee.mapper.CollegeMapper;
import com.bigegg.employee.mapper.MajorMapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bigegg.employee.mapper.GraduateMapper;
import com.bigegg.employee.service.IGraduateService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 毕业生管理Service业务层处理
 *
 * @author 刘博
 * @date 2022-08-11
 */
@Service
public class GraduateServiceImpl implements IGraduateService {

    public final Logger log = LoggerFactory.getLogger(GraduateServiceImpl.class);

    @Autowired
    private GraduateMapper graduateMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 查询毕业生管理
     *
     * @param id 毕业生管理主键
     * @return 毕业生管理
     */
    @Override
    public Graduate selectGraduateById(Long id) {
        return graduateMapper.selectGraduateById(id);
    }

    /**
     * 查询毕业生管理列表
     *
     * @param graduate 毕业生管理
     * @return 毕业生管理
     */
    @Override
    public List<Graduate> selectGraduateList(Graduate graduate) {
        return graduateMapper.selectGraduateList(graduate);
    }

    @Autowired
    private CUserService cUserService;

    @Autowired
    SysUserServiceImpl userService;


    /**
     * 新增毕业生管理
     *
     * @param graduate 毕业生管理
     * @return 结果
     */
    @Override
    public int insertGraduate(Graduate graduate) {
        int c = graduateMapper.insertGraduate(graduate);
        if (c > 0) {
            RegisterInfo info = new RegisterInfo();
            info.setUsername(graduate.getStuNo());
            info.setPassword("000000");
            info.setNickname(graduate.getName());
            cUserService.registerTypeUser(2, info);
        }
        return c;
    }

    /**
     * 修改毕业生管理
     *
     * @param graduate 毕业生管理
     * @return 结果
     */
    @Override
    public int updateGraduate(Graduate graduate) {
        return graduateMapper.updateGraduate(graduate);
    }

    /**
     * 批量删除毕业生管理
     *
     * @param ids 需要删除的毕业生管理主键
     * @return 结果
     */
    @Override
    public int deleteGraduateByIds(Long[] ids) {
        return graduateMapper.deleteGraduateByIds(ids);
    }

    /**
     * 删除毕业生管理信息
     *
     * @param id 毕业生管理主键
     * @return 结果
     */
    @Override
    public int deleteGraduateById(Long id) {
        return graduateMapper.deleteGraduateById(id);
    }

    /**
     * 毕业生人数统计
     */
    @Override
    public Graduate getGraCnt(Graduate graduate) {
        return graduateMapper.getGraCnt(graduate);
    }

    /**
     * 按班级统计人数
     */
    @Override
    public List<Graduate> getGraCntByClasses(List<Long> classIdList) {
        return graduateMapper.getGraCntByClasses(classIdList);
    }

    /**
     * 按学院专业统计人数
     */
    @Override
    public List<Graduate> getGraCntByCollegeAndMajor(Graduate graduate) {
        return graduateMapper.getGraCntByCollegeAndMajor(graduate);
    }

    /**
     * 按专业统计人数
     */
    @Override
    public List<Graduate> getGraCntByMajor(Graduate graduate) {
        return graduateMapper.getGraCntByMajor(graduate);
    }

    /**
     * 上传excel
     */
    @Override
    public void importData(MultipartFile multipartFileile){

        InputStream is = null;
        try {
            is = multipartFileile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Graduate> list = EasyExcel.read(new BufferedInputStream(is)).head(Graduate.class).sheet().headRowNumber(1).doReadSync();

        if(list!=null && list.size()>0){

            Long cnt = 0L;

            for(Graduate graduate : list){

                //
                Graduate c = new Graduate();

                c.setStuNo(graduate.getStuNo());

                List<Graduate> graduateList = this.graduateMapper.selectGraduateList(c);

                if(graduateList==null || graduateList.size()==0){

                    graduate.setCreateTime(new Date());

                    // 学院id
                    College college = new College();
                    college.setName(graduate.getCollegeName());
                    List<College> collegeList = this.collegeMapper.selectCollegeList(college);
                    if(collegeList!=null && collegeList.size()>0){
                        College col = collegeList.get(0);
                        graduate.setCollegeId(col.getId());
                    }

                    // 专业
                    Major major = new Major();
                    major.setName(graduate.getMajorName());
                    List<Major> majorList = this.majorMapper.selectMajorList(major);
                    if(majorList!=null && majorList.size()>0){
                        Major col = majorList.get(0);
                        graduate.setMajorId(col.getId());
                    }

                    // 班级
                    Classes classes = new Classes();
                    classes.setName(graduate.getClassesName());
                    List<Classes> classesList = this.classesMapper.selectClassesList(classes);
                    if(classesList!=null && classesList.size()>0){
                        Classes cl = classesList.get(0);
                        graduate.setClassesId(cl.getId());
                    }

                    // 状态为1激活
                    graduate.setState(1);

                    int re = this.graduateMapper.insertGraduate(graduate);

                    if (re > 0) {
                        RegisterInfo info = new RegisterInfo();
                        info.setUsername(graduate.getStuNo());
                        info.setPassword("000000");
                        info.setNickname(graduate.getName());
                        cUserService.registerTypeUser(2, info);
                    }

                }else {

                    this.log.info("此条已存在，无需新增，进行关联");
                }

                cnt++;
                this.log.info(String.format("已完成%d/%d",cnt,list.size()));
            }

        }
    }
}
